package com.kitty.mybatis.mybatisv1.binding;

import com.kitty.mybatis.mybatisv1.session.KittySqlSession;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author SHEN
 * @Date 2020/10/21
 */
public class KittyMapperProxyFactory<T> {

    private final Class<T> mapperInterface;

    private Map<Method, KittyMapperMethod> mapperMethodMap = new ConcurrentHashMap<>();

    public KittyMapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public Class<T> getMapperInterface() {
        return mapperInterface;
    }

    public Map<Method, KittyMapperMethod> getMapperMethodMap() {
        return mapperMethodMap;
    }


    protected T newInstance(KittyMapperProxy mapperProxy){
        return (T)Proxy.newProxyInstance(mapperInterface.getClassLoader(),new Class[] {mapperInterface},mapperProxy);
    }

    public T newInstance(KittySqlSession sqlSession) {
        final KittyMapperProxy mapperProxy = new KittyMapperProxy(sqlSession,mapperInterface,mapperMethodMap);
        return this.newInstance(mapperProxy);
    }
}
